import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  JobInstanceApi,
  JobInstanceReq,
  JobInstanceModel,
  JobInstancePage,
  JobInstanceKey,
} from "./types";

class JobInstanceApiImpl implements JobInstanceApi {
  getKey(model: JobInstanceModel): JobInstanceKey {
    return {
      classify: model.classify,
      instanceName: model.instanceName,
    } as JobInstanceKey;
  }

  // 查询翻页信息运行实例;
  findPage(query?: JobInstanceReq): AxiosPromise<JobInstancePage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasState = query.state != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_instance/page/" + tenant,
      method: "get",
      params: query,
    });
  }
}
const jobInstanceApi = new JobInstanceApiImpl();

export { jobInstanceApi };
